package cn.yotta.user.core.web.controller.admin;

import cn.yotta.common.api.model.IdRequest;
import cn.yotta.common.api.model.IdsRequest;
import cn.yotta.common.api.model.Paging;
import cn.yotta.user.api.params.user.UserPageRequest;
import cn.yotta.user.api.params.user.UserStatusChangeRequest;
import cn.yotta.user.api.vo.UserVo;
import cn.yotta.user.core.server.service.UserService;
import cn.yotta.user.core.web.utils.ApiExecutor;
import cn.yotta.user.web.autoconfiguration.context.user.UserContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author wyaoyao
 * @date 2023/3/15 17:17
 */
@RestController
@RequestMapping("user/admin/")
@Api(tags = "用户服务")
public class UserAdminController {

    private final UserService userService;

    public UserAdminController(UserService userService) {
        this.userService = userService;
    }

    @ApiOperation("【批量】根据用户id查询用户信息")
    @GetMapping("/withIds")
    public List<UserVo> findUserByIds(IdsRequest idsRequest) {
        return ApiExecutor.execute(userService::findUserByIds, idsRequest);
    }

    @ApiOperation("根据用户id查询用户信息")
    @GetMapping("/{id}")
    public UserVo findUserById(@PathVariable(name = "id") Long id) {
        final IdRequest idRequest = new IdRequest();
        idRequest.setId(id);
        return ApiExecutor.execute(userService::findUserById, idRequest);
    }

    @ApiOperation("用户分页查询")
    @GetMapping("/page")
    public Paging<UserVo> page(UserPageRequest pageRequest) {
        return ApiExecutor.execute(userService::page, pageRequest);
    }

    @ApiOperation("冻结用户")
    @PutMapping("/freeze/{id}")
    public Boolean freeze(@PathVariable(name = "id") Long id) {
        final UserStatusChangeRequest request = new UserStatusChangeRequest();
        request.setOperatorId(UserContext.getUserId());
        request.setId(id);
        return ApiExecutor.execute(userService::freeze, request);
    }

    @ApiOperation("解冻用户")
    @PutMapping("/unfreeze/{id}")
    public Boolean unFreeze(@PathVariable(name = "id") Long id) {
        final UserStatusChangeRequest request = new UserStatusChangeRequest();
        request.setOperatorId(UserContext.getUserId());
        request.setId(id);
        return ApiExecutor.execute(userService::unFreeze, request);
    }

    @ApiOperation("启用用户")
    @PutMapping("/enable/{id}")
    public Boolean enable(@PathVariable(name = "id") Long id) {
        final UserStatusChangeRequest request = new UserStatusChangeRequest();
        request.setOperatorId(UserContext.getUserId());
        request.setId(id);
        return ApiExecutor.execute(userService::enable, request);
    }

    @ApiOperation("禁用用户")
    @PutMapping("/disable/{id}")
    public Boolean disable(@PathVariable(name = "id") Long id) {
        final UserStatusChangeRequest request = new UserStatusChangeRequest();
        request.setOperatorId(UserContext.getUserId());
        request.setId(id);
        return ApiExecutor.execute(userService::disable, request);
    }


}
